#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   dns_service.sh
# Version:    1.0
# Date:       2013/10/16
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-06DNS-01字符模式-01DNS服务控制与信息查询-02查询DNS服务日志信息
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/10/16
#             - Start named service, check log.
# ----------------------------------------------------------------------

##! @TODO: start&stop named service
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_test()
{
    serv=named
    CONF=/etc/named.conf

    OWNER=`ls -l ${CONF} | awk '{print $3,$4}'`
    if [ "${OWNER}" != "named named" ]; then
        chown named:named ${CONF}
        EchoResult "check owner of ${CONF}"
    fi

    RNDCKEY="/etc/rndc.key"
    OWNER=`ls -l ${RNDCKEY} | awk '{print $3,$4}'`
    if [ "${OWNER}" != "named named" ]; then
        chown named:named ${RNDCKEY}
        EchoResult "check owner of ${RNDCKEY}"
    fi

    LOG="/var/log/messages"

    status_daemon ${serv}

    # start
    if [ $? -ne 0 ]
    then
        echo "${serv} is stopped"
        start_daemon ${serv}
    else
        echo "${serv} is running..."
    fi 

    sleep 3
    # check httpd start
    status_daemon ${serv}  && netstat -nlutp | grep ${serv}
    EchoResult "service ${serv} start"

    # check log messages
    PID_NAMED=`pidof ${serv}`
    grep "named\[${PID_NAMED}\]: running" ${LOG}
    EchoResult "${serv} start messages is logged"

    # stop
    service ${serv} stop

    # check httpd stop
    ! netstat -nlutp | grep ${serv}
    EchoResult "service ${serv} stop"

    # check log messages
    grep "named\[${PID_NAMED}\]: exiting" ${LOG}
    EchoResult "${serv} stop messages is logged"

}


# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh

do_test || EchoResult "testcase: ${serv} start & stop log messages."
echo ""
